package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.dao.LitemallActivityGrouponMapper;
import org.linlinjava.litemall.db.domain.LitemallActivityGroupon;
import org.linlinjava.litemall.db.domain.LitemallActivityGrouponExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class ActivityGrouponService {

    private final Log logger = LogFactory.getLog(ActivityGrouponService.class);

    @Resource
    private LitemallActivityGrouponMapper activityGrouponMapper;

    public LitemallActivityGroupon findById(Integer id) {
        LitemallActivityGrouponExample example = new LitemallActivityGrouponExample();
        example.or().andIdEqualTo(id);
        return activityGrouponMapper.selectOneByExample(example);
    }

    /**
     * 添加团购活动用户信息
     *
     * @param param
     */
    public void insertActivityGroupon(LitemallActivityGroupon param) {
        param.setAddTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        param.setDeleted(LitemallActivityGroupon.NOT_DELETED);
        activityGrouponMapper.insert(param);
    }

    public List<LitemallActivityGroupon> pageQuery(Integer tag, Integer activityId, Integer userId, Byte userType,
                                                   Integer subjectId, String subject, String grouponCode, String userName, Byte state,
                                                   Integer page, Integer limit, String sort, String order) {
        LitemallActivityGrouponExample example = new LitemallActivityGrouponExample();
        LitemallActivityGrouponExample.Criteria criteria = example.createCriteria();

        if (activityId != null) {
            criteria.andActivityIdEqualTo(activityId);
        }
        if (state != null) {
            criteria.andStateEqualTo(state);
        }
        if (userType != null) {
            criteria.andUserTypeEqualTo(userType);
        }
        if (tag != null) {
            if (tag == 1) {
                // userId不限
                criteria.andGrouponIdEqualTo(0);
            } else if (tag == 2) {
                criteria.andUserIdEqualTo(userId.toString());
            }
        }
        if (subjectId != null) {
            criteria.andSubjectIdEqualTo(subjectId);
        }
        if (!StringUtils.isEmpty(subject)) {
            criteria.andSubjectLike(subject);
        }
        if (!StringUtils.isEmpty(grouponCode)) {
            criteria.andGrouponCodeEqualTo(grouponCode);
        }
        if (!StringUtils.isEmpty(userName)) {
            criteria.andUserNameLike(userName);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return activityGrouponMapper.selectByExample(example);
    }

    public List<LitemallActivityGroupon> queryListForExport(Integer tag, Integer activityId, Integer userId, Byte userType,
                                                            Integer subjectId, String subject, String grouponCode, String userName,
                                                            Byte state) {
        LitemallActivityGrouponExample example = new LitemallActivityGrouponExample();
        LitemallActivityGrouponExample.Criteria criteria = example.createCriteria();

        if (activityId != null) {
            criteria.andActivityIdEqualTo(activityId);
        }
        if (state != null) {
            criteria.andStateEqualTo(state);
        }
        if (userType != null) {
            criteria.andUserTypeEqualTo(userType);
        }
        if (tag != null) {
            if (tag == 1) {
                // userId不限
                criteria.andGrouponIdEqualTo(0);
            } else if (tag == 2) {
                criteria.andUserIdEqualTo(userId.toString());
            }
        }
        if (subjectId != null) {
            criteria.andSubjectIdEqualTo(subjectId);
        }
        if (!StringUtils.isEmpty(subject)) {
            criteria.andSubjectLike(subject);
        }
        if (!StringUtils.isEmpty(grouponCode)) {
            criteria.andGrouponCodeEqualTo(grouponCode);
        }
        if (!StringUtils.isEmpty(userName)) {
            criteria.andUserNameLike(userName);
        }

        example.setOrderByClause("groupon_code desc, groupon_id asc");
        return activityGrouponMapper.selectByExample(example);
    }

    public List<LitemallActivityGroupon> findByParams(List<Map<String, Object>> params) {
        if (CollectionUtils.isEmpty(params)) {
            return Collections.emptyList();
        }
        return activityGrouponMapper.findByParams(params);
    }

    public LitemallActivityGroupon findByOrderId(Integer orderId) {
        if (orderId == null || orderId == 0) {
            return null;
        }
        LitemallActivityGrouponExample example = new LitemallActivityGrouponExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(LitemallActivityGroupon.NOT_DELETED);
        return activityGrouponMapper.selectOneByExample(example);
    }

    public int updateById(LitemallActivityGroupon entity) {
        entity.setUpdateTime(LocalDateTime.now());
        LitemallActivityGrouponExample example = new LitemallActivityGrouponExample();
        example.or().andIdEqualTo(entity.getId());
        return activityGrouponMapper.updateByExampleSelective(entity, example);
    }
}
